#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
ll m,n,k,k2,t,sum,cnt=1,cnt1;
ll n1,ans1,pd,sum1,maxm=-1,ans2,q;
ll x,y,x2,y2,d,ans;
string s1;
void init()
{
    ans1=0;
    ans2=0;
    cnt=0;
}
void solve()
{
	init();//m行n列 
    cin>>m>>n>>x>>y>>x2>>y2>>s1; //x,y球的起始坐标 //x2 y2最终 
    for(int i=1;;i++)
    {
		if(s1=="DL")
		{
			if(x2-x==y-y2&&x2-x>=0)
			{
				cout<<cnt<<endl;
				return ;
			}
			else
			{
				ans=min(m-x,y-1);
				x+=ans,y-=ans;
                  if(x==m&&y==1&&i!=1)
				{
					cout<<"-1"<<endl;
					return ;
				}
				else if(x==m&&y==1&&i==1) s1="UR",cnt++;
				else if(x==m) s1="UL",cnt++;
				else if(y==1) s1="DR",cnt++;
			}
		}
		else if(s1=="UR")
		{
			if(x-x2==y2-y&&x-x2>=0)
			{
				cout<<cnt<<endl;
				return ;
			}
			else
			{
				ans=min(n-y,x-1);
				x-=ans,y+=ans;
				if(x==1&&y==n&&i!=1)
				{
 					cout<<"-1"<<endl;
 					return ;                   					
				}
				else if(x==1&&y==m&&i==1) s1="DL",cnt++;
				else if(x==1) s1="DR",cnt++;
				else if(y==n) s1="UL",cnt++;
 			}
		}
		else if(s1=="DR")
		{
			if(x2-x==y2-y&&x2-x>=0)
			{
				cout<<cnt<<endl;
				return ;
			}
			else
			{
				ans=min(m-x,n-y);
				x+=ans,y+=ans;
				if(x==m&&y==n&&i!=1)
				{
					cout<<"-1"<<endl;
					return ;
				}
				else if(x==m&&y==n&&i==1) s1="UL",cnt++;
				else if(x==m) s1="UR",cnt++;
				else if(y==n) s1="DL",cnt++;
 			}
		}
		else if(s1=="UL")
		{
			if(x-x2==y-y2&&x-x2>=0)
			{
				cout<<cnt<<endl;
				return ;
			}
			else
			{
				ans=min(x-1,y-1);
				x-=ans,y-=ans;
				if(x==1&&y==1&&i!=1)
				{
					cout<<"-1"<<endl;
					return ;
				}
				else if(x==1&&y==1&&i==1) s1="DR",cnt++;
				else if(x==1) s1="DL",cnt++;
				else if(y==1) s1="UR",cnt++;
 			}			
		}
//		cout<<s1<<" "<<cnt<<" "<<i<<endl;
	}
}
int main()
{
//	int t=1;
     IOS;
    cin>>t;
   while(t--)
  {
	solve();
  }

}